<?php

/**
 * taskModel short summary.
 *
 * taskModel description.
 *
 * @version 1.0
 * @author Marie
 */
class taskModel extends CI_Model
{

	public function __construct() {
            parent::__construct();
            $this->load->library('Gantt/Task');
            $this->load->library('Gantt/Item');
            $this->load->library('Gantt/Schedule');
            $this->load->library('Gantt/Categorie');
            $this->load->library('User');
        }
        public function readAllByGroup(int $idGroup){
		$query = $this->selectFullTask();
        $query = $this->db->where('tm_user_usr.fk_grp',$idGroup);
        $query = $this->db->get();

        $rows = $query->result();

        return $this->fillFullTask($rows);
	}

	public function readAllByUser(int $idUser){
		$query = $this->selectFullTask();
        $query = $this->db->where('tm_user_usr.pk_usr',$idUser);
        $query = $this->db->get();

        $rows = $query->result();

        return $this->fillFullTask($rows);
	}

	private function selectFullTask(){
		$startQuery =  $this->db->select('pk_tsk, tsk_lib, tsk_comment, tsk_start_hour_plan, tsk_end_hour_plan, tsk_start_hour_real, tsk_end_hour_real, 
									tsk_state, tsk_is_np ,pk_itm, itm_lib, itm_weight, itm_priority, pk_cat, cat_lib ' );
		$startQuery = $this->db->from('tm_task_tsk');
		$startQuery = $this->db->join('ref_item_itm', 'tm_task_tsk.fk_itm = ref_item_itm.pk_itm');
		$startQuery = $this->db->join('tm_user_usr', 'tm_task_tsk.fk_usr = tm_user_usr.pk_usr');
		$startQuery = $this->db->join('ref_categorie_cat', 'tm_task_tsk.fk_cat = ref_categorie_cat.pk_cat');
		//$query = $this->db->join('TM_GROUP_GRP', 'TM_GROUP_GRP.pk_grp = tm_user_usr.fk_grp');
	}


	private function fillFullTask($rows){
        foreach ($rows as $key => $data) {
        	$user = new User();
        	$categorie = new Categorie($data->pk_cat, $data->cat_lib);
        	$item = new Item($data->pk_itm, $data->itm_lib, $data->itm_weight, $data->itm_priority, $categorie);
        	$planification = new Schedule($data->tsk_start_hour_plan, $data->tsk_end_hour_plan, $data->tsk_start_hour_real, $data->tsk_end_hour_real);
        	$task = new Task($data->pk_tsk, $data->tsk_lib, $data->tsk_comment, $item, $planification, $user);

        	$result[$data->pk_tsk] = $task;
        }
        return $result;
	}
    
    
}
